﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TXIO;
using System.Text;

public partial class UControls_WUC_UpFile : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lil_title.Text = typeTitle;
            btn_up.Attributes["OnClick"] += "Page_ValidationActive=false;";
        }
    }

    protected void btn_up_Click(object sender, EventArgs e)
    {
        if (fup_up.HasFile)
        {
            //验证文件类型
            string fileExten = new System.IO.FileInfo(fup_up.FileName).Extension;
            if (!ValidateFileType(fileExten))
            {
                txt_ServerPath.Text = "文件应该为：" + fileType;
                return;
            }
            //验证文件大小
            if (!ValidateFileSize(fup_up.PostedFile.ContentLength))
            {
                txt_ServerPath.Text = "文件应小于：" + fileSize.ToString() + "K";
                return;
            }


            string uploadDir = saveDir;
            string fullName = string.Empty;
            string GetTimeFileName = Guid.NewGuid().ToString();

            if (isAutoName)
            {
                uploadDir += DateTime.Now.ToString("yyyy-MM-dd");
                //组合新文件名
                fullName = uploadDir + "/" + GetTimeFileName + fileExten.ToLower();
            }
            else
            {
                fullName = uploadDir + fup_up.FileName;
            }

            //获取物理目录
            //string imgdir = UIMethod.GetAppSettings("ImagesDir");
            //获取虚拟目录
            string virdir = UIMethod.ApplicationPath();
            string upload = UIMethod.GetAppSettings("ImagesDNS");
            string imgdir = Server.MapPath(virdir) + upload;

            //设置临时存放地址
            string tempPath = imgdir + GetTimeFileName + fup_up.FileName;
            //设置新地址
            string savePath = imgdir + fullName;
            //创建文件夹
            Txt_IO.CreateDirectory(imgdir + uploadDir.ToString());
            //显示相对全路径
            txt_ServerPath.Text = virdir + upload + fullName.ToString();


            if (isChangeSize != false || waterMark != 0)
            {
                //保存文件
                fup_up.PostedFile.SaveAs(tempPath);

                WaterMarkProcess(tempPath, savePath);
            }
            else
            {
                //保存文件
                fup_up.PostedFile.SaveAs(savePath);
            }
        }
        else
        {
            txt_ServerPath.Text = "请选择一个本地文件";
        }
    }

    #region 成员
    protected string fileType = string.Empty;
    protected int fileSize = 200;
    protected string typeTitle = "图片：";
    protected string saveDir = string.Empty;
    protected bool isAutoName = true;

    protected bool isChangeSize = false;
    protected int up_Width = 200;
    protected int up_Height = 150;

    protected int waterMark = 0;
    /// <summary>
    /// 图片水印的地址
    /// </summary>
    protected string wm_imgPath = UIMethod.GetAppSettings("ImagesDir") + UIMethod.GetAppSettings("WaterMark_Image");
    /// <summary>
    /// 文字水印的文本
    /// </summary>
    protected string wm_text = UIMethod.GetAppSettings("WaterMark_Text");
    #endregion

    #region 属性
    /// <summary>
    /// 获取上传结果的文本框
    /// </summary>
    public string GetFileClientID
    {
        get { return txt_ServerPath.ClientID; }
    }
    /// <summary>
    /// 是否改变图片大小
    /// </summary>
    public bool IsChangeSize
    {
        set { isChangeSize = value; }
    }
    /// <summary>
    /// 设置水印类型
    /// </summary>
    public int WaterMark
    {
        set { waterMark = value; }
    }
    /// <summary>
    /// 设置上传图片的宽度
    /// </summary>
    public int UP_Width
    {
        set { up_Width = value; }
    }
    /// <summary>
    /// 设置上传图片的高度
    /// </summary>
    public int UP_Height
    {
        set { up_Height = value; }
    }
    /// <summary>
    /// 设置是否更新名称
    /// </summary>
    public bool IsAutoName
    {
        set { isAutoName = value; }
    }
    /// <summary>
    /// 获取上传后的新文件路径
    /// </summary>
    public string Text
    {
        get { return txt_ServerPath.Text; }
        set { txt_ServerPath.Text = value; }
    }
    /// <summary>
    /// 文件类型 例：JPG|GIF|BMP
    /// </summary>
    public string FileType
    {
        set { fileType = value.ToLower(); }
    }
    /// <summary>
    /// 类型标题
    /// </summary>
    public string TypeTitle
    {
        set { typeTitle = value + "："; }
    }
    /// <summary>
    /// 文件大小
    /// </summary>
    public int FileSize
    {
        set { fileSize = value; }
    }
    /// <summary>
    /// 保存目录路径
    /// </summary>
    public string SaveDirectory
    {
        set { saveDir = value; }
    }
    /// <summary>
    /// 获取上传控件
    /// </summary>
    public FileUpload GetUploadControl
    {
        get { return fup_up; }
    }
    #endregion

    #region 私有方法
    protected void WaterMarkProcess(string _tempPath, string _newPath)
    {
        ImageHandler IH = new ImageHandler(_tempPath, _newPath);
        IH.IsChangeSize = isChangeSize;
        IH.Img_Width = up_Width;
        IH.Img_Height = up_Height;
        ImageHandler.WaterMarkType wmt = (ImageHandler.WaterMarkType)waterMark;
        IH.WaterMark = wmt;
        switch (wmt)
        {
            case ImageHandler.WaterMarkType.Bmp:
                IH.WM_Text_BmpPath = wm_imgPath;
                break;
            case ImageHandler.WaterMarkType.Text:
                IH.WM_Text_BmpPath = wm_text;
                break;
        }
        IH.Processing();

        //删除文件
        Txt_IO.DeleteFile(_tempPath);
    }

    /// <summary>
    /// 验证文件类型
    /// </summary>
    /// <param name="ExtensionName">扩展名</param>
    /// <returns></returns>
    protected bool ValidateFileType(string ExtensionName)
    {
        ExtensionName = ExtensionName.ToLower();
        if (fileType == "*") { return true; }
        string[] extenarray = fileType.ToLower().Split('|');
        foreach (string temp in extenarray)
        {
            if (temp.Trim() == ExtensionName)
            {
                return true;
            }
        }
        return false;
    }
    /// <summary>
    /// 验证文件大小
    /// </summary>
    /// <param name="ContentLength">文件长度</param>
    /// <returns></returns>
    protected bool ValidateFileSize(int ContentLength)
    {
        if (ContentLength != 0)
        {
            if ((ContentLength / 1024) <= fileSize)
            {
                return true;
            }
            return false;
        }
        return true;
    }
    #endregion
}
